package com.jiangSheng.mall.app.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.jiangSheng.mall.app.bean.ConfirmOrder;
import com.jiangSheng.mall.app.bean.ShoppingCar;
import com.jiangSheng.mall.app.bean.Users;
import com.jiangSheng.mall.app.services.ShoppingCarServices;
import com.jiangSheng.mall.app.services.UserServices;
import com.jiangSheng.mall.app.utils.CommonResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api("购物车方法")
@RequestMapping("/shoppingCar")
public class ShoppingCarController {
	
		@Autowired
		private ShoppingCarServices shoppingCarServices;
		@Autowired
		private UserServices userServices;
		
	 	@ApiOperation("添加商品到购物车")
	    @RequestMapping(value = "/scSave", method = RequestMethod.POST)
	    public CommonResult<Integer> scSave(@RequestBody ShoppingCar shoppingCar) {
	        int flag = shoppingCarServices.scSave(shoppingCar);
	        if (flag > 0) {
	            return CommonResult.success(flag);
	        }
	        return CommonResult.failed();
	    }
	 	@ApiOperation("获取本机用户的购物车列表")
	    @RequestMapping(value = "/scList", method = RequestMethod.GET)
	    public CommonResult<List<ShoppingCar>> scList() {
	 		List<ShoppingCar> sclist = shoppingCarServices.scList(userServices.getUser().getId());
	 		return CommonResult.success(sclist);
	    }
	 	
	 	@ApiOperation("根据选中的产品获取购物车中产品的信息列表")
	    @RequestMapping(value = "/scList", method = RequestMethod.GET)
	    public CommonResult<List<ShoppingCar>> getScListById(List<Integer> ids) {
	 		List<ShoppingCar> sclist = shoppingCarServices.getScListById(ids);
	 		return CommonResult.success(sclist);
	    }
	 	
	 	@ApiOperation("修改本机用户的购物车信息")
	 	@RequestMapping(value = "/scAlter" ,method = RequestMethod.PUT)
	 	public CommonResult<Integer> scAlter(@RequestBody ShoppingCar shoppingCar) {
	 		Users user = userServices.getUser();
	 		shoppingCar.setUserId(user.getId());
	 		shoppingCar.setUserName(user.getName());
	 		int flag = shoppingCarServices.scAlter(shoppingCar);
	 		if(flag > 0) {
	 			return CommonResult.success(flag);
	 		}else {
	 			return CommonResult.failed();
	 		}
	 	}
	 	@ApiOperation("删除购物车中的商品")
	 	@RequestMapping(value = "/scMove", method = RequestMethod.DELETE)
	 	public CommonResult<Integer> scMove(List<Integer> ids){
	 		if(ids.size() == 0) {
	 			return CommonResult.failed();
	 		}
	 		int flag = shoppingCarServices.scMove(ids);
	 		if(flag > 0) {
	 			return CommonResult.success(flag);
	 		}else {
	 			return CommonResult.failed();
	 		}
	 		
	 	}
	 	@ApiOperation("购物车选中商品结算并生成确认订单")
	 	@RequestMapping(value = "/scSettleAndGenerateConfirmOrder", method = RequestMethod.POST)
	 	public CommonResult<ConfirmOrder> scSettleAndGenerateOrder(List<Integer> ids){
	 		ConfirmOrder confirmOrder = shoppingCarServices.scSettleAndGenerateConfirmOrder(ids);
	 		return CommonResult.success(confirmOrder);
	 		
	 	}
}
